package com.xindaoapp.happypet.activity;

import java.io.File;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.ConstantsValue;
import com.xindaoapp.happypet.fragments.ConsigneeFragment;
import com.xindaoapp.happypet.fragments.MessageCenterFragment;
import com.xindaoapp.happypet.utils.BitmapHelp;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.SharePrefUtil;
import com.xindaoapp.happypet.view.CircleImageView;

public class UserActivity extends UMBaseActivity implements OnClickListener {
    @ViewInject(R.id.userinfo)
    private RelativeLayout userinfo;
    @ViewInject(R.id.user_tiezi)
    private RelativeLayout user_tiezi;
    @ViewInject(R.id.user_guanzhu)
    private RelativeLayout user_guanzhu;
    @ViewInject(R.id.user_fans)
    private RelativeLayout user_fans;

    @ViewInject(R.id.my_order_1)
    private RelativeLayout my_order_1;
    @ViewInject(R.id.my_order_2)
    private RelativeLayout my_order_2;
    @ViewInject(R.id.my_order_3)
    private RelativeLayout my_order_3;
    @ViewInject(R.id.my_order_4)
    private RelativeLayout my_order_4;

    @ViewInject(R.id.my_order)
    private RelativeLayout my_order;
    @ViewInject(R.id.my_cats)
    private RelativeLayout my_cats;
    @ViewInject(R.id.my_address)
    private RelativeLayout my_address;

    @ViewInject(R.id.cancel)
    private ImageButton cancel;
    @ViewInject(R.id.publish)
    private ImageButton publish;

    @ViewInject(R.id.user_touxiang)
    private CircleImageView user_touxiang;
    @ViewInject(R.id.user_name)
    private TextView user_name;
    @ViewInject(R.id.iv_tishi)
    private ImageView iv_tishi;

    /**
     * 粉丝后面小红点
     */
    @ViewInject(R.id.fans_push)
    private ImageView fans_push;


    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        sp = getSharedPreferences("userconfig", MODE_PRIVATE);
        ViewUtils.inject(this);
        initClick();
        initBitMapUtils();
    }

    private void initClick() {
        userinfo.setOnClickListener(this);
        user_tiezi.setOnClickListener(this);
        user_guanzhu.setOnClickListener(this);
        user_fans.setOnClickListener(this);

        my_order_1.setOnClickListener(this);
        my_order_2.setOnClickListener(this);
        my_order_3.setOnClickListener(this);
        my_order_4.setOnClickListener(this);

        my_order.setOnClickListener(this);
        my_cats.setOnClickListener(this);
        my_address.setOnClickListener(this);

        publish.setOnClickListener(this);
        cancel.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        if (!UserState.isLogged()) {
            finish();
        }
        initUserTouxiangAndName(UserState.isLogged());
        boolean updatenew = SharePrefUtil.getBoolean(getBaseContext(), "updatenew", false);
        if (updatenew) {
            iv_tishi.setVisibility(View.VISIBLE);
        } else {
            iv_tishi.setVisibility(View.INVISIBLE);
        }
        super.onResume();
    }

    /**
     * 初始化用户的头像和名字
     */
    private void initUserTouxiangAndName(boolean isLogin) {
        if (isLogin) {
            user_name.setText(UserState.getUser().username);
            if (ConstantsValue.ISUPLOADSUCCESS) {
                if (!TextUtils.isEmpty(sp.getString("uid", ""))) {
                    File filepath = new File(Environment.getExternalStorageDirectory(), "temp");
                    File file = new File(filepath, "userpic.jpg");
                    Bitmap decodeFile = BitmapFactory.decodeFile(file.getAbsolutePath());
                    user_touxiang.setImageBitmap(decodeFile);
                }
            } else {
                if (CommonParameter.isLoadImg) {
                    BitmapHelp.getBitmapUtils(this).display(user_touxiang,
                            UserState.getUser().userface, config);
                } else {
                    user_touxiang.setImageResource(R.drawable.unlogin_logo);
                }
            }
        }
    }

    private BitmapDisplayConfig config;

    private BitmapDisplayConfig initBitMapUtils() {
        config = new BitmapDisplayConfig();
        config.setLoadingDrawable(getResources().getDrawable(R.drawable.person_loading));
        config.setLoadFailedDrawable(getResources().getDrawable(R.drawable.person_loading));
        return config;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.publish:
                Intent publish = new Intent(this, SettingActivity.class);
                startActivity(publish);
                break;
            case R.id.cancel:
                finish();
                break;
            case R.id.userinfo:
                Intent userinfo = new Intent(this, UserInfoActivity.class);
                startActivity(userinfo);
                break;
            case R.id.user_tiezi:
                Intent user_tiezi = new Intent(this, MyPostActivity.class);
                startActivity(user_tiezi);
                break;
            case R.id.user_guanzhu:
                Intent user_guanzhu = new Intent(this, MenuAttention.class);
                startActivity(user_guanzhu);
                break;
            case R.id.user_fans:
                Intent user_fans = new Intent(this, FansActivity.class);
                startActivity(user_fans);
                break;
            case R.id.my_order_1:
                Intent my_order_1 = new Intent(this, MyLoveActivity.class);
                startActivity(my_order_1);
                break;
            case R.id.my_order_2:
                Intent my_order_2 = new Intent(this, MyMessageActivity.class);
                startActivity(my_order_2);
                break;
            case R.id.my_order_3:
                Intent my_order_3 = new Intent(this, MyShouCangActivity.class);
                startActivity(my_order_3);
                break;
            case R.id.my_order_4:
                Intent my_order_4 = new Intent(this, MainActivity.class);
                my_order_4.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        MessageCenterFragment.class.getCanonicalName());
                startActivity(my_order_4);
                break;
            case R.id.my_order:
                Intent my_order = new Intent(this, MyOrderListActivity.class);
                startActivity(my_order);
                break;
            case R.id.my_cats:
                Intent my_cats = new Intent(this, MyShopCollectActivity.class);
                startActivity(my_cats);
                break;
            case R.id.my_address:
                Intent my_address = new Intent(this, MainActivity.class);
                my_address.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        ConsigneeFragment.class.getCanonicalName());
                startActivity(my_address);
                break;

            default:
                break;
        }
    }

}
